package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.CccxDaoljc;
import com.zr.model.Course;
import com.zr.model.Teacher;
import com.zr.util.JDBCUtil;

public class CccxDaoImplljc implements CccxDaoljc{
	//分页查询所有的课程信息
	@Override
	public List<Course> getAllCourseByPageAndRows(int page, int rows) {
		List<Course> course = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT course.cid,course.cname,course.classtime,course.place,teacher.tname");
		sql.append(" FROM course INNER JOIN teacher");
		sql.append(" ON course.tid = teacher.tid");
		sql.append(" LIMIT ?,?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, (page-1)*rows);
			pst.setInt(2, rows);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Course c = new Course();
				c.setCid(rs.getInt("cid"));
				c.setCname(rs.getString("cname"));
				c.setClasstime(rs.getString("classtime"));
				c.setPlace(rs.getString("place"));
				c.setTname(rs.getString("tname"));
				course.add(c);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return course;
	}
	
	
	//课程的总个数
	@Override
	public int countAllCourse() {
		int count = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(course.tid) as count ");
		sql.append(" FROM course");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				count =Integer.parseInt(rs.getString("count"));
			}	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

}
